const express = require('express');
const router = express.Router();
const userController = require('../controllers/userController.js');
const { requirePermission } = require('../middlewares/roleMiddleware.js');

router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

/**
 * GET /api/users
 * @security BearerAuth
 * @header {string} Authorization - Bearer Token认证信息，格式为: Bearer {token}
 * @summary 获取用户列表
 * @tags user
 * @param {number} page.query.required - 页码 
 * @param {number} pageSize.query.required - 每页数量 
 * @param {string} searchKey.query - 搜索关键词 
 * @return {object} 200 - success
 * @example response - 200 - success response example
 * [
 *   {
 *     "id": 1,
 *     "name": "admin",
 *     "email": "admin@example.com"
 *   }
 * ]
 */
router.get('/users', userController.getUsers);

/**
 * POST /api/users
 * @security BearerAuth
 * @header {string} Authorization - Bearer Token认证信息，格式为: Bearer {token}
 * @summary 新增用户
 * @tags user
 * @param {object} request.body - 请求体参数
 * @param {user} request.body.user - 用户对象
 * @return {object} 200 - success
 */
router.post('/users', requirePermission(['user:create','role:assign']), userController.addUser);
/**
 * POST /api/users/batch
 * @security BearerAuth
 * @header {string} Authorization - Bearer Token认证信息，格式为: Bearer {token}
 * @summary 批量新增用户
 * @tags user
 * @param {object} request.body.required - 请求体参数
 * @param {string} request.body.filePath - {filePath:url} 上传的文件的路径
 * @return {object} 200 - success
 */
router.post('/users/batch', requirePermission(['user:create','role:assign']),userController.batchAddUser);
/**
 * PUT /api/users/batch
 * @security BearerAuth
 * @header {string} Authorization - Bearer Token认证信息，格式为: Bearer {token}
 * @summary 批量更新用户
 * @tags user
 * @param {object} request.body.required - 请求体参数
 * @param {Array} request.body.ids - 用户ID列表
 * @param {String} request.body.updateType - 更新类型
 * @param {Object} request.body.updateValue - 更新值
 * @return {object} 200 - success
 */
router.put('/users/batch', requirePermission(['user:update','role:assign']), userController.batchUpdateUser);
/**
 * PUT /api/users/{id}
 * @security BearerAuth
 * @header {string} Authorization - Bearer Token认证信息，格式为: Bearer {token}
 * @summary 更新用户
 * @tags user
 * @param {string} id.path - 用户ID
 * @return {object} 200 - success
 */
router.put('/users/:id', requirePermission(['user:update','role:assign']), userController.updateUser);

/**
 * DELETE /api/users/batch
 * @security BearerAuth
 * @header {string} Authorization - Bearer Token认证信息，格式为: Bearer {token}
 * @summary 批量删除用户
 * @tags user
 * @param {Array} request.body - 用户ID列表
 * @return {object} 200 - success
 */
router.delete('/users/batch', requirePermission('user:delete'), userController.batchDeleteUser);
/**
 * DELETE /api/users/{id}
 * @security BearerAuth
 * @header {string} Authorization - Bearer Token认证信息，格式为: Bearer {token}
 * @summary 删除用户
 * @tags user
 * @param {string} id.path - 用户ID
 * @return {object} 200 - success
 */
router.delete('/users/:id', requirePermission('user:delete'), userController.deleteUser);

// 导出路由
module.exports = router;
